package demo1105.demo01;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/5 9:43
 */
public class Test01 {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪",18,"中国",'F'));
        personList.add(new Person("Tom",24,"美国",'M'));
        personList.add(new Person("Harley",22,"英国",'F'));
        personList.add(new Person("向天笑",20,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("小梅",20,"中国",'F'));
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李康2",22,"中国",'M'));

        String[] arr1 = { "a", "b", "c", "d" };
        String[] arr2 = { "d", "e", "f", "g" };
        Stream<String> stream1 = Stream.of(arr1);
        Stream<String> stream2 = Stream.of(arr2);

        //personList.stream().filter(item->item.getAge()>20).forEach(System.out::println);

        //personList.stream().map(item->item.getName()).forEach(System.out::println);

        //personList.stream().sorted((o1,o2)->Integer.compare(o1.getAge(),o2.getAge())).forEach(System.out::println);

        //personList.stream().sorted((o1,o2)->(o1.getAge()-o2.getAge())).forEach(System.out::println);

        //合并流
        //Stream.concat(stream1,stream2).forEach(System.out::println);

        //去重
        //Stream.concat(stream1,stream2).distinct().forEach(System.out::println);

        //获取前n个元素
        //Stream.concat(stream1,stream2).limit(3).forEach(System.out::println);

        //跳过前n个元素
        //Stream.concat(stream1,stream2).skip(3).forEach(System.out::println);

        //find操作
        //Optional<Person> first = personList.stream().findFirst();
        //System.out.println(first);

        //Optional<Person> any = personList.stream().findAny();
        //System.out.println(any);

        //match操作
        //boolean b = personList.stream().allMatch(item -> item.getAge() > 1);
        //System.out.println(b);

        //boolean b1 = personList.stream().anyMatch(item -> item.getAge() > 23);
        //System.out.println(b1);

        //聚合操作
//        long count = personList.stream().count();
//        System.out.println(count);
//
//        Optional<Person> max = personList.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
//        System.out.println(max);
//
//        Optional<Person> min = personList.stream().min((o1, o2) -> o1.getAge() - o2.getAge());
//        System.out.println(min);

        //reduce规约
          Integer[] arr = {1,2,3,4,5};
//        Optional<Integer> reduce = Arrays.stream(arr).reduce((i1, i2) -> i1 += i2);
//        System.out.println(reduce.get());

        //collect
//        Long collect = personList.stream().collect(Collectors.counting());
//        System.out.println(collect);

//        Double collect1 = personList.stream().collect(Collectors.averagingInt(item -> item.getAge()));
//        System.out.println(collect1);

//        Optional<Person> collect2 = personList.stream().collect(Collectors.maxBy((o1, o2) -> (o1.getAge() - o2.getAge())));
//        System.out.println(collect2);
//
//        Integer collect3 = personList.stream().collect(Collectors.summingInt(item -> item.getAge()));
//        System.out.println(collect3);

        //搜集为一个新的集合
        Set<String> collect4 = personList.stream().filter(item -> item.getAge() > 20).map(item -> item.getName()).collect(Collectors.toSet());
        System.out.println(collect4);

        Map<String, Person> collect5 = personList.stream().filter(item -> item.getAge() > 20).collect(Collectors.toMap(item -> item.getName(), item -> item));
        System.out.println(collect5);

        //分组
        Map<Character, List<Person>> collect6 = personList.stream().collect(Collectors.groupingBy(item -> item.getSex()));
        System.out.println(collect6);
    }
}
class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
